package ldh.im.fxbase.dao;

import ldh.im.fxbase.data.pojo.ImLogin;
import ldh.im.fxbase.data.pojo.ext.ImLoginExt;
import ldh.im.fxbase.util.DbUtil;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ImLoginDao extends BaseDao {

    private static final String TABLE_NAME = "im_cache_login_user";
    private static final String COLUMNS = "id, userId, token, maskPassword, loginType loginTypeValue, updateTime";

    public ImLogin getLoginByUserId(String userId) throws SQLException {
        return DbUtil.query((queryRunner, connection) -> {
            String sql = String.format("select %s from im_cache_login_user where userId = ? order by id desc", COLUMNS);
            ImLoginExt imLogin = queryRunner.query(connection, sql, new BeanHandler<>(ImLoginExt.class), userId);
            return imLogin;
        });
    }

    public ImLogin getLoginForLasted() throws SQLException {
        return DbUtil.query((queryRunner, connection) -> {
            String sql = String.format("select %s from im_cache_login_user where token = '' order by updateTime desc limit 0, 1", COLUMNS);
            ImLogin imLogin = queryRunner.query(connection, sql, new BeanHandler<>(ImLogin.class));
            return imLogin;
        });
    }

    public void insertLogin(ImLogin imLogin) throws SQLException {
        LOGGER.info("insert user token, userId: {}, token: {}", imLogin.getUserId(), imLogin.getToken());
        DbUtil.transaction((queryRunner, connection) -> {
            ResultSetHandler<Long> resultSetHandler = new ScalarHandler();
            String sql = String.format("insert into im_cache_login_user(userId, token, maskPassword, loginType, updateTime) values(?, ?, ?, ?, ?)");
            Long id = queryRunner.insert(connection, sql, resultSetHandler, imLogin.getUserId(), imLogin.getToken(), imLogin.getMaskPassword(),
                    imLogin.getLoginType().getValue(), imLogin.getUpdateTime());
            return imLogin;
        });
    }

    public List<ImLogin> getLogins() throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select %s from im_cache_login_user where loginType in (1, 2) order by updateTime desc", COLUMNS);
            return queryRunner.query(connection, sql, new BeanListHandler<>(ImLoginExt.class));
        });
    }

    public void updateLogin(ImLogin imLogin) throws SQLException {
        LOGGER.info("update user token, userId: {}, token: {}", imLogin.getUserId(), imLogin.getToken());
        DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("update im_cache_login_user set token = ?, loginType = ?, maskPassword = ?, updateTime  = ? where userId = ?");
            queryRunner.update(connection, sql, imLogin.getToken(), imLogin.getLoginType().getValue(), imLogin.getMaskPassword(), imLogin.getUpdateTime(), imLogin.getUserId());
            return imLogin;
        });
    }
}
